<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <!--添加 CSRF 支持-->
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
    <title>用户注册页面</title>
    <link th:href="@{/login/css/bootstrap1.css}" rel="stylesheet">
    <script th:src="@{/login/js/bootstrap1.js}"></script>
    <script th:src="@{/login/js/jquery.js}"></script>
</head>
<body>
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h3>用户注册</h3>
                </div>
                <div class="card-body">
                    <!-- 消息提示 -->
                    <div th:if="${msg}" class="alert alert-success">
                        <span th:text="${msg}"></span>
                    </div>
                    <div th:if="${error}" class="alert alert-danger">
                        <span th:text="${error}"></span>
                    </div>

                    <!-- 注册表单（绑定User实体） -->
                    <form th:action="@{/doRegister}" method="post" th:object="${user}">
                        <!-- 用户名 -->
                        <div class="mb-3">
                            <label class="form-label">用户名</label>
                            <input type="text" th:field="*{username}" class="form-control" placeholder="请输入用户名">
                        </div>

                        <!-- 密码 -->
                        <div class="mb-3">
                            <label class="form-label">密码</label>
                            <input type="password" th:field="*{password}" class="form-control" placeholder="请输入密码">
                        </div>

                        <!-- 邮箱 -->
                        <div class="mb-3">
                            <label class="form-label">邮箱</label>
                            <input type="email" th:field="*{email}" class="form-control" placeholder="请输入邮箱">
                        </div>

                        <!-- 验证码 -->
                        <div class="mb-3">
                            <div class="input-group">
                                <input type="text" name="verifyCode" class="form-control" placeholder="请输入验证码">
                                <button type="button" id="sendCodeBtn" class="btn btn-secondary">获取验证码</button>
                            </div>
                        </div>

                        <!-- 提交 -->
                        <button type="submit" class="btn btn-primary">注册</button>
                        <a th:href="@{/toLogin}" class="btn btn-link">已有账号？登录</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 验证码发送逻辑
    $(function() {
        // 获取 CSRF token 和 header name
        const csrfToken = $('meta[name="_csrf"]').attr('content');
        const csrfHeader = $('meta[name="_csrf_header"]').attr('content');
        // 设置 AJAX 全局默认请求头（推荐）
        $.ajaxSetup({
            beforeSend: function(xhr) {
                if (csrfToken && csrfHeader) {
                    xhr.setRequestHeader(csrfHeader, csrfToken);
                }
            }
        });

        let countdown = 60;
        let isSending = false;

        $("#sendCodeBtn").click(function() {
            const email = $("input[name='email']").val();
            if (!email) {
                alert("请输入邮箱");
                return;
            }
            if (isSending) return;

            isSending = true;
            $(this).prop("disabled", true).text("发送中...");


            $.post("/sendCode", { email: email }, function(result) {
                if (result === "success") {
                    alert("验证码已发送");
                    let timer = setInterval(function() {
                        countdown--;
                        $("#sendCodeBtn").text(countdown + "秒后重发");
                        if (countdown <= 0) {
                            clearInterval(timer);
                            $("#sendCodeBtn").prop("disabled", false).text("获取验证码");
                            countdown = 60;
                            isSending = false;
                        }
                    }, 1000);
                } else {
                    alert(result);
                    $("#sendCodeBtn").prop("disabled", false).text("获取验证码");
                    isSending = false;
                }
            });
        });
    });
</script>
</body>
</html>